package com.cinema.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 放映厅类
 * 
 * @author Cinema Management System
 * @version 1.0.0
 */
public class CinemaHall {
    
    /**
     * 放映厅ID
     */
    private String hallId;
    
    /**
     * 放映厅名称
     */
    private String hallName;
    
    /**
     * 排数
     */
    private int rows;
    
    /**
     * 每排座位数
     */
    private int seatsPerRow;
    
    /**
     * 座位列表
     */
    private List<Seat> seats;
    
    /**
     * 默认构造函数
     */
    public CinemaHall() {
        this.seats = new ArrayList<>();
    }
    
    /**
     * 构造函数
     * 
     * @param hallId 放映厅ID
     * @param hallName 放映厅名称
     * @param rows 排数
     * @param seatsPerRow 每排座位数
     */
    public CinemaHall(String hallId, String hallName, int rows, int seatsPerRow) {
        this();
        this.hallId = hallId;
        this.hallName = hallName;
        this.rows = rows;
        this.seatsPerRow = seatsPerRow;
        initializeSeats();
    }
    
    /**
     * 初始化座位
     */
    private void initializeSeats() {
        seats.clear();
        for (int row = 1; row <= rows; row++) {
            for (int col = 1; col <= seatsPerRow; col++) {
                Seat seat = new Seat(row, col, hallId);
                seats.add(seat);
            }
        }
    }
    
    /**
     * 获取指定位置的座位
     * 
     * @param row 排数
     * @param col 列数
     * @return 座位对象，如果不存在返回null
     */
    public Seat getSeat(int row, int col) {
        for (Seat seat : seats) {
            if (seat.getRow() == row && seat.getColumn() == col) {
                return seat;
            }
        }
        return null;
    }
    
    /**
     * 获取总座位数
     * 
     * @return 总座位数
     */
    public int getTotalSeats() {
        return rows * seatsPerRow;
    }
    
    /**
     * 获取可用座位数
     * 
     * @return 可用座位数
     */
    public int getAvailableSeats() {
        return (int) seats.stream().filter(Seat::isAvailable).count();
    }
    
    /**
     * 获取已占用座位数
     * 
     * @return 已占用座位数
     */
    public int getOccupiedSeats() {
        return getTotalSeats() - getAvailableSeats();
    }
    
    // Getter和Setter方法
    public String getHallId() {
        return hallId;
    }
    
    public void setHallId(String hallId) {
        this.hallId = hallId;
    }
    
    public String getHallName() {
        return hallName;
    }
    
    public void setHallName(String hallName) {
        this.hallName = hallName;
    }
    
    public int getRows() {
        return rows;
    }
    
    public void setRows(int rows) {
        this.rows = rows;
    }
    
    public int getSeatsPerRow() {
        return seatsPerRow;
    }
    
    public void setSeatsPerRow(int seatsPerRow) {
        this.seatsPerRow = seatsPerRow;
    }
    
    public List<Seat> getSeats() {
        return new ArrayList<>(seats);
    }
    
    public void setSeats(List<Seat> seats) {
        this.seats = seats;
    }
    
    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        CinemaHall that = (CinemaHall) obj;
        return Objects.equals(hallId, that.hallId);
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(hallId);
    }
    
    @Override
    public String toString() {
        return "CinemaHall{" +
                "hallId='" + hallId + '\'' +
                ", hallName='" + hallName + '\'' +
                ", rows=" + rows +
                ", seatsPerRow=" + seatsPerRow +
                ", totalSeats=" + getTotalSeats() +
                ", availableSeats=" + getAvailableSeats() +
                '}';
    }
} 